package com.hsy.sunflowerbao.home.action

import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import com.flux.action.SubscriptionManager
import com.flux.dispatcher.Dispatcher
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.action.BaseActionCreator
import com.hsy.sunflowerbao.base.action.BaseParam
import com.hsy.sunflowerbao.base.action.Key
import com.hsy.sunflowerbao.base.http.RetrofitFactory

/**
 * 首页行为生成器
 * Created on 2018/3/26.
 *
 * @author Yang
 */
class HomeActionCreator(dispatcher: Dispatcher, manager: SubscriptionManager) : BaseActionCreator(dispatcher, manager) {

    private val service = RetrofitFactory.getService(HomeService::class.java)

    /**
     * 获取首页数据
     */
    fun getHomeData(type: String, page: String, tab: String) {
        val action = newAction(ActionType.GET_HOME_DATA + tab)

        val params = HomeParam(type, page)
        service.getHomeData(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.HOME_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 根据二维码获取用户信息
     * @param msg 二维码内容
     */
    fun scanUser(msg: String) {
        val action = newAction(ActionType.GET_SCAN_USER)

        val params = UserParam(msg)
        service.scanUser(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", "该用户不存在") }
                .map { result -> result.user }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.USER_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 根据二维码获取用户信息
     * @param msg 二维码内容
     */
    fun searchUser(msg: String) {
        val action = newAction(ActionType.GET_SEARCH_USER)

        val params = UserParam(msg)
        service.searchUser(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", "该用户不存在") }
                .map { result -> result.user }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.USER_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 根据二维码获取用户信息
     * @param id 二维码内容
     * @param uid 二维码内容
     */
    fun checkCoupon(id: String, uid: String) {
        val action = newAction(ActionType.CHECK_COUPON)

        val params = CheckCouponParam(id, uid)
        service.checkCoupon(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", result.msg) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ result ->
                    action.data[Key.CHECK_RESULT] = result
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 获取标签
     */
    fun getTags() {
        val action = newAction(ActionType.GET_TAGS)

        val params = BaseParam()
        service.getTags(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.TAG_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }

    /**
     * 赠送积分
     */
    fun sendScore(uid: String, money: String, tags: String) {
        val action = newAction(ActionType.SEND_SCORE)

        val params = ScoreParam(uid, money, tags)
        service.sendScore(params.toMap())
                .subscribeOn(Schedulers.io())
                .filter { response -> checkResult(response.error, "0", response.msg) }
                .map { response -> response.result }
                .filter { result -> checkResult(result.done, "1", result.msg) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ homeData ->
                    action.data[Key.TAG_DATA] = homeData
                    postRxAction(action)
                }, { throwable ->
                    handleHttpError(throwable)
                    action.success = false
                    postRxAction(action)
                })
    }
}